//
//  UIImageView+imageId.m
//  seafishing2
//
//  Created by zhaoyk10 on 13-12-5.
//  Copyright (c) 2013年 Szfusion. All rights reserved.
//
#import "UIImageView+imageId.h"
#import "Aspects.h"

#define SCREEN_SCALE [UIScreen mainScreen].scale
#define VIP_IDENTIFIER @"VIP_IDENTIFIER"

@implementation UIImageView (imageId)
@dynamic yhVipImg;
@dynamic defaultLayer;

- (void)setImageWithImageId:(NSString *)imageId
{
    [self setImageWithImageId:imageId size:CGSizeMake(self.width, self.height) isNew:NO];
}

- (void)setImageWithImageId:(NSString *)imageId isNew:(BOOL)isNew
{
        [self setImageWithImageId:imageId size:CGSizeMake(self.width, self.height) isNew:isNew];
}

- (void)setImageWithImageId:(NSString *)imageId placeHolder:(UIImage *)placeHolderImage
{
    [self setImageWithImageId:imageId size:CGSizeMake(self.width, self.height) placeHolder:placeHolderImage];
}

- (void)setImageWithImageId:(NSString *)imageId size:(CGSize)size
{
    [self setImageWithImageId:imageId size:size isNew:NO];
}

- (void)setImageWithImageId:(NSString *)imageId size:(CGSize)size isNew:(BOOL)isNew
{
    [self setImageWithImageId:imageId size:size placeHolder:nil isNew:isNew];
}

- (void)setImageWithImageId:(NSString *)imageId size:(CGSize)size placeHolder:(UIImage *)placeHolderImage
{
    [self setImageWithImageId:imageId size:size placeHolder:placeHolderImage isNew:NO];
}

- (void)setImageWithImageId:(NSString *)imageId size:(CGSize)size  placeHolder:(UIImage *)placeHolderImage isNew:(BOOL)isNew
{
    [self setImageWithImageId:imageId size:size placeHolder:placeHolderImage isNew:isNew completed:nil];
}

- (void)setImageWithImageId:(NSString *)imageId size:(CGSize)size placeHolder:(UIImage *)placeHolderImage completed:(SDWebImageCompletionBlock)completedBlock
{
    [self setImageWithImageId:imageId size:size placeHolder:placeHolderImage isNew:NO completed:completedBlock];
}

- (void)setImageWithImageId:(NSString *)imageId size:(CGSize)size placeHolder:(UIImage *)placeHolderImage isNew:(BOOL)isNew completed:(SDWebImageCompletionBlock)completedBlock
{
    if(placeHolderImage == nil)
    {
//        placeHolderImage = [UIImage imageNamed:@"default_bg.jpg"];//给个默认图
    }
    CGFloat s = [UIScreen mainScreen].scale;
    size = CGSizeMake(size.width*s, size.height*s);
    self.defaultLayer.hidden = NO;
    if(!self.defaultLayer)
    {
        self.defaultLayer = [CALayer layer];
        self.defaultLayer.contents = (__bridge id)[UIImage imageNamed:@"default_image_big"].CGImage;
        self.defaultLayer.contentsGravity = kCAGravityResize;
        @weakify(self);
        [self aspect_hookSelector:@selector(layoutSubviews) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo){
            @strongify(self);
            UIImageView *img = aspectInfo.instance;
        
            //以最小边长计算
            CGFloat s = 100.f/200.f;
            
            //图片原始282x267
            CGFloat edge = MIN(img.width, img.height);
            CGFloat w = edge * s;
            CGFloat h = w * (267.f/282.f);
            self.defaultLayer.frame = CGRectMake((img.width - w)/2, (img.height - h)/2, w, h);
            
        } error:nil];
        
        [self aspect_hookSelector:@selector(setImage:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo,UIImage *image){
            @strongify(self);
            if(image)
            {
                self.defaultLayer.hidden = YES;
            }
        } error:nil];

        [self.layer addSublayer:self.defaultLayer];
    }
    NSURL *url = nil;
    
//    if (![NSString isBlankString:imageId] && [imageId rangeOfString:@"http://msg.catches.com/"].location != NSNotFound) {
//        imageId = [imageId stringByReplacingOccurrencesOfString:@"http://msg.catches.com/" withString:@""];
//        url = [NSURL URLWithString:[YHImageUrl msg_imageUrlForId:imageId size:size]] ;
//    } else {
//        if(!isNew)
//        {
//
//            url = [NSURL URLWithString:[YHImageUrl webpCropURL:imageId withSize:size]];
//        }
//        else
//        {
//            url = [NSURL URLWithString:[YHImageUrl webpFitURL:imageId withSize:size]];
//
//        }
//    }
    [self sd_setImageWithURL:url placeholderImage:placeHolderImage options:0 completed:completedBlock];
}

- (void)setRoundImageWithImageId:(NSString *)imageId placeHolder:(UIImage *)placeHolderImage
{
    CGSize viewSize = self.size;
    @weakify(self);
    [self setImageWithImageId:imageId size:CGSizeMake(self.width, self.height) placeHolder:placeHolderImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        @strongify(self);
        UIImage *newImage;
        CGFloat cornerRadius = viewSize.height/2.0f;
        CGRect frame = CGRectMake(0, 0, viewSize.width, viewSize.height);
        UIGraphicsBeginImageContextWithOptions(viewSize, NO, [UIScreen mainScreen].scale);
        [[UIBezierPath bezierPathWithRoundedRect:frame
                                    cornerRadius:cornerRadius] addClip];
        [image drawInRect:frame];
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        self.image = newImage;
    }];
}

- (void)setImageWithAvatar:(NSString *)imageId
{

    [self setImageWithAvatar:imageId isRound:NO];
}

- (void)setRoundImageWithAvatar:(NSString *)imageId
{
    [self setImageWithAvatar:imageId isRound:YES];
}

- (void)setImageWithAvatar:(NSString *)imageId  isRound:(BOOL)isRound
{
    UIImageView *vipImg = self.yhVipImg;
    if(!vipImg)
    {
        vipImg = [UIImageView new];
        vipImg.image = [UIImage imageNamed:@"VIP"];
        [[self superview] addSubview:vipImg];
        self.yhVipImg = vipImg;
//        vipImg.hq_ratio(1);
//        vipImg.hq_layout().width().equalTo(self).width().ratio(0.25).end();

        if (!isRound) {
//
//            vipImg.hq_layout().bottom().equalTo(self).bottom().end();
//            vipImg.hq_layout().right().equalTo(self).right().end();
        } else {

//            vipImg.hq_layout().bottom().equalTo(self).bottom().end();
//            vipImg.hq_layout().right().equalTo(self).right().end();
         
        }
        
    }
    
//    if([NSString isBlankString:imageId])
//    {
//        vipImg.hidden = YES;
//        self.image = [UIImage imageNamed:@"avatar"];
//    }
//    else
//    {
//        BOOL isVip = YES;
//        NSString *isVipStr;
//        long long expire = 0;
//        NSScanner *scanner = [NSScanner scannerWithString:imageId];
//        [scanner scanUpToString:@"_" intoString: nil];
//        [scanner scanString:@"_" intoString:nil];
//        [scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@"_"] intoString:&isVipStr];
//        [scanner scanUpToString:@"_" intoString:nil];
//        [scanner scanString:@"_" intoString:nil];
//        [scanner scanLongLong:&expire];
//        if(isVipStr)
//        {
//            isVip = [isVipStr boolValue];
//            if(isVip)
//            {
//                if(expire < [[NSDate date] timeIntervalSince1970] * 1000)
//                {
//                    isVip = NO;
//                }
//            }
//        }
//        else
//        {
//            isVip = NO;
//        }
//        
//        vipImg.hidden = !isVip;
//        [self setImageWithImageId:imageId size:CGSizeMake(96, 96) placeHolder:[UIImage imageNamed:@"avatar"]];
//    }
}

- (void)hideVIP:(BOOL)hide
{
    self.yhVipImg.hidden = hide;
}

#pragma mark - vip图标
- (UIImageView *)yhVipImg
{
    return (UIImageView *)objc_getAssociatedObject(self, @selector(yhVipImg));
}

- (void)setYhVipImg:(UIImageView *)yhVipImg
{
    objc_setAssociatedObject(self, @selector(yhVipImg), yhVipImg, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

#pragma mark - 默认图
- (CALayer *)defaultLayer
{
    return (CALayer *)objc_getAssociatedObject(self, @selector(defaultLayer));;
}

- (void)setDefaultLayer:(CALayer *)defaultLayer
{
    objc_setAssociatedObject(self, @selector(defaultLayer), defaultLayer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
